package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.domain.MarketGoodsExample;
import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.db.domain.MarketTopicExample;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketTopicMapper;
import com.cskaoyan.market.service.AdminTopicService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class AdminTopicServiceImpl implements AdminTopicService {

    @Autowired
    MarketTopicMapper topicMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;


    @Override
    public List<MarketTopic> list(Integer page, Integer limit, String sort, String order, String title, String subtitle) {
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        if(!StringUtils.isEmpty(title)){
            criteria.andTitleLike("%" + title + "%");
        }
        if(!StringUtils.isEmpty(subtitle)){
            criteria.andSubtitleLike("%" + subtitle + "%");
        }
        marketTopicExample.setOrderByClause(sort+" "+order);
        PageHelper.startPage(page,limit);
        List<MarketTopic> marketTopics=topicMapper.selectByExample(marketTopicExample);
        return marketTopics;
    }

    @Override
    @Transactional
    public int delete(Integer id) {

        int i = topicMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public Map<String, Object> read(Integer id) {
        Map<String,Object> objectMap=new HashMap<>();
        // topic
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<MarketTopic> marketTopics=topicMapper.selectByExample(marketTopicExample);
        objectMap.put("topic",marketTopics.get(0));

        // goodsList
        MarketGoodsExample marketGoodsExample=new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria1=marketGoodsExample.createCriteria();
        Integer[] goods = marketTopics.get(0).getGoods();
        for (Integer good : goods) {
            criteria1.andIdEqualTo(good);
        }
        List<MarketGoods> marketGoods=goodsMapper.selectByExample(marketGoodsExample);
        objectMap.put("goodsList",marketTopics);
        return objectMap;
    }

    @Override
    @Transactional
    public int create(MarketTopic marketTopic) {

        int i = topicMapper.insert(marketTopic);
        return i;
    }

    @Override
    @Transactional
    public int update(MarketTopic marketTopic) {
        int i = topicMapper.updateByPrimaryKey(marketTopic);
        return i;
    }
}
